<?php namespace App\Utils;

use Exception;

class DeviceTool {

    //获取用户的设备信息
    public static function getDeviceSystem() {
        $request = app('request');
        $userAgent = $request->header('user-agent');
        $system = '';
        //检测是不是mac
        if(stripos($userAgent, 'Macintosh')) {
            $system = "mac系统";
        }
        //检测是不是windows
        if(stripos($userAgent, 'Windows')) {
            $system = "windows系统";
        }
        //检测是不是ubuntu
        if(stripos($userAgent, 'Ubuntu')) {
            $system = 'ubuntu系统';
        }
        //检测是不是iphone
        if(stripos($userAgent, 'iPhone')) {
            $system = 'iphone手机';
        }
        //检测是不是ipad
        if(stripos($userAgent, 'iPad')) {
            $system = 'ipad';
        }
        //检测是不是andriod手机
        if(stripos($userAgent, 'Android')) {
            $system = 'android手机';
        }
        if(empty($system)) {
            $system = '其他设备';
        }
        return $system;
    }
    //判断用户是不是手机访问
    public static function isMobile() {
        $request = app('request');
        $found = stristr($request->header('user-agent'), 'mobile');
        if($found) {
            return true;
        } else {
            return false;
        }
    }
    //判断是不是在手机微信中访问的
    public static function isMicroMessenger() {
        $request = app('request');
        $found = stristr($request->header('user-agent'), 'MicroMessenger');
        if($found) {
            return true;
        } else {
            return false;
        }
    }
    //通过ip获取定位信息（使用qq的api）
    public static function getIPLocQQ($queryIP) {
        try {
            $url = 'http://ip.qq.com/cgi-bin/searchip?searchip1=' . $queryIP;
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_ENCODING, 'gb2312');
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
            $result = curl_exec($ch);
            $result = mb_convert_encoding($result, "utf-8", "gb2312"); // 编码转换，否则乱码
            curl_close($ch);
            preg_match("@<span>(.*)</span></p>@iU", $result, $ipArray);
            $loc = $ipArray[1];
            return $loc;
        } catch (Exception $e) {
            return "";
        }
    }
    //通过Ip获取地址（高德）
    public static function getCityByIp($ip) {
        $key = "77095839e7a388d0a59f32ae121ea2f5";
        $url = "http://restapi.amap.com/v3/ip?key=" . $key . "&ip=" . $ip;
        try {
            $result = file_get_contents($url);
            $arr = json_decode($result, true);
            if($arr['status'] == '1') {
                $data = [
                    'province' => $arr['province'],
                    'city' => $arr['city'],
                ];
                return ['status'=>'success', 'data'=>$data ];
            } else {
                return ['status'=>'failed', 'message'=>'参数错误'];
            }
        } catch(Exception $e) {
            return  ['status'=>'failed', 'message'=>'参数错误'];
        }
    }
    //查询手机号码归属地
    public static function qCellCore($mobile) {
        try {
            $url = "http://sj.apidata.cn/?mobile=" . $mobile;
            $result = file_get_contents($url);
            if(!$result) {
                return ['status'=>'failed', 'message'=>"获取信息失败"];
            }
            $arr = json_decode($result, true);
            if($arr['status'] == '1') {
                //data":{"prefix":187,"province":"江西","city":"南昌","isp":"移动","code":791,"zipcode":330008,"types":"中国移动 GSM\/3G","mobile":"18779866705"}
                return ['status'=>'success', 'data'=>$arr['data']];
            } else {
                return ['status'=>'failed', 'message'=>"获取信息失败"];
            }
        } catch(Exception $e) {
            return ['status'=>'failed', 'message'=>"获取信息失败"];
        }
    }

    //通过手机号码获取归属地
    public static function getMobileHomeLocation($mobile) {
        //淘宝网API
        try {
            $apiurl = 'http://apis.juhe.cn/mobile/get';
            $params = array(
                'key' => 'b4b88a8ffc09e2fd3f24251ee19fa168', //您申请的手机号码归属地查询接口的appkey
                'phone' => $mobile //要查询的手机号码
            );
            $paramsString = http_build_query($params);
            $content = @file_get_contents($apiurl.'?'.$paramsString);
            $result = json_decode($content,true);
            if(isset($result['resultcode']) && ($result['resultcode'] == '200')) {
                $data = $result['result'];
                $city = $data['city'];
                return ['status'=>'success', 'data'=>$city];
            } else {
                return ['status'=>'failed', 'message'=>'获取信息失败'];
            }
        } catch(Exception $e) {
            return  ['status'=>'failed', 'message'=>'获取信息失败'];
        }
    }
    //手机号码归属地
    public static function getMobileHomeLocationByK780($mobile) {
        try {
            $result = file_get_contents("http://api.k780.com:88/?app=phone.get&phone=".$mobile."&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json");
            $data = json_decode($result, true);
            $cityName = '';
            if(isset($data['result']['style_citynm'])) {
                $cityString = $data['result']['style_citynm'];
                $cityArray = explode(',', $cityString);
                $count = count($cityArray);
                if($count>0) {
                    $cityName = $cityArray[$count-1];
                }
            }
            return ['status'=>'success', 'data'=>$cityName];
        } catch(Exception $e) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
    }
}

?>